package data

import (
	"context"
	"fmt"
	"go_private_im/internal/logic/biz"
)




func (l *LogicRepo) RoomExist(ctx context.Context,roomId int64) (room *biz.RoomDetailInfo,err error) {
	room = &biz.RoomDetailInfo{}
	if err = l.data.Db.Table("room").Where("id = ? and status = 0",roomId).Take(room).Error; err != nil {
		return
	}
	return
}


func (l *LogicRepo) GetRoom(ctx context.Context,roomId string,userId int64) (roomInfo *biz.RoomInfo,err error) {
	roomInfo = &biz.RoomInfo{}
	if err = l.data.Db.Table("room").Where("id = ? and user_id = ? and status = 0",roomId,userId).Take(roomInfo).Error; err != nil {
		return
	}
	return
}

func (l *LogicRepo) GetRoomsByRoomId(ctx context.Context,roomId string) (roomInfo biz.RoomInfo,err error) {
	if err = l.data.Db.Table("room").Where("id = ? and status = 0",roomId).Take(&roomInfo).Error; err != nil {
		fmt.Println(err)
		return
	}
	return
}

func (l *LogicRepo) GetRoomsByUserId(ctx context.Context,userId int64) (roomInfo []*biz.RoomInfo,err error) {
	roomInfo = []*biz.RoomInfo{}
	if err = l.data.Db.Table("room").Where("user_id = ? and status = 0",userId).Find(roomInfo).Error; err != nil {
		return
	}
	return
}

func (l *LogicRepo) DelRoom(ctx context.Context,roomId string) (err error) {
	roomInfo := &biz.RoomInfo{}
	if err = l.data.Db.Table("room").Where("id = ? and status = 0",roomId).Delete(roomInfo).Error; err != nil {
		return
	}
	return
}

func (l *LogicRepo) DelRooms(ctx context.Context,roomId []string) (err error) {
	roomInfo := &biz.RoomInfo{}
	if err = l.data.Db.Table("room").Where("id in ? and status = 0",roomId).Delete(roomInfo).Error; err != nil {
		return
	}
	return
}


func (l *LogicRepo) UpdataRoom(ctx context.Context,roomInfo *biz.RoomInfo) (err error) {
	if err = l.data.Db.Table("room").Updates(roomInfo).Error; err != nil {
		return
	}
	return
}

func (l *LogicRepo) UserExistRoom(ctx context.Context,roomId string,userId int64) bool {
	if _,err := l.GetRoom(ctx,roomId,userId); err != nil {
		return false
	}
	return true
}

